package com.springboot.springboot4.controller;

import com.springboot.springboot4.Bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @ClassName:
 * @author: CKC
 * @DateTime: 2021/1/30 10:20
 */
@Slf4j
@Controller
public class IndexController {
    /**
     * 来登录页
     *
     * @return
     */
    @GetMapping(value = {"/", "/login"})
    public String loginPage() {
        return "login";
    }
    
    @PostMapping(value = "/login")
    public String main(User user, HttpSession session, Model model) {
        if (StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())) {
            //吧登录成功的用户保存起来
            session.setAttribute("loginUser", user);
            //  登录成功重定向到main.html
            return "redirect:main.html";
        } else {
            model.addAttribute("msg", "账号或密码错误~");
            //回到登录页
            return "login";
        }
    }
    
    /**
     * 去main页面
     *
     * @return
     */
    @GetMapping("main.html")
    public String mainPage(HttpSession session) {
        //是否登录，     拦截器，过滤器
        log.info("当前方法是{}", "mainPage");
        return "main";
    }
    
}
